package systemController

import (
	"CommonsProject/pkg/tools/redisTool"
	"context"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/wenlng/go-captcha/captcha"
	"net/http"
	"time"
)

var Captcha *captcha.Captcha

// 初始化验证码部分
func init() {
	Captcha = captcha.GetCaptcha()
}

// GeneratorCaptcha 生成图片
func (controller *CommonsController) GeneratorCaptcha(ctx *gin.Context) {

	pointInfo, base64, btBase, uuid, err := Captcha.Generate()

	if err != nil {
		panic(errors.New("生成验证码失败！" + err.Error()))
	}

	background := context.Background()
	if err = redisTool.ClientRedis.Set(background, fmt.Sprintf("%s:%s", redisTool.CAPTCHA_VERIFY, uuid), pointInfo, time.Minute).Err(); err != nil {
		panic(errors.New("Redis连接失败！" + err.Error()))
	}

	ctx.JSON(http.StatusOK, controller.SuccessData(gin.H{
		"bigImage":   base64,
		"minImage":   btBase,
		"verifyUUID": uuid,
	}))
}
